package com.wzx.ptplatform.service.impl;

import com.wzx.ptplatform.dao.UniversityDao;
import com.wzx.ptplatform.domain.Company;
import com.wzx.ptplatform.domain.University;
import com.wzx.ptplatform.domain.User;
import com.wzx.ptplatform.service.UniversityService;
import com.wzx.ptplatform.utils.PtplatformException;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.persistence.criteria.Expression;
import javax.persistence.criteria.Predicate;
import java.util.List;

/**
 * @author 紫煦桉
 */
@Service
public class UniversityServiceImpl implements UniversityService {

    @Resource
    private UniversityDao universityDao;

    @Override
    public List<University> findAll() throws PtplatformException {
        return universityDao.findAll();
    }

    @Override
    public Page<University> findList(University university, Integer page, Integer limit) throws PtplatformException {
        Specification<University> spec = (Specification<University>) (root, criteriaQuery, criteriaBuilder) -> {
            Predicate predicate = criteriaBuilder.conjunction();
            List<Expression<Boolean>> exList = predicate.getExpressions();//动态SQL表达式集合
            if (university.getUniversityName() != null) {
                exList.add(criteriaBuilder.like(root.get("universityName").as(String.class), "%" + university.getUniversityName() + "%"));
            }
            if (university.getCity() != null && university.getCity().getId() != null) {
                exList.add(criteriaBuilder.equal(root.get("city").get("id").as(Long.class), university.getCity().getId()));
            }
            return predicate;
        };
        PageRequest pageable = PageRequest.of(page, limit, Sort.Direction.DESC, "id");
        return universityDao.findAll(spec, pageable);
    }

    @Override
    public University findById(long id) throws PtplatformException {
        return universityDao.findById(id).orElse(null);
    }

    @Override
    public boolean saveOrEdit(University university) throws PtplatformException {
        try {
            universityDao.saveAndFlush(university);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public boolean deleteById(long id) throws PtplatformException {
        try {
            universityDao.deleteById(id);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    @Override
    public University login(University university) throws PtplatformException {
        University universityMessage = universityDao.findByEmail(university.getEmail());
        if (universityMessage != null && universityMessage.getPassword().equals(university.getPassword())) {
            return universityMessage;
        }
        return null;
    }

    @Override
    public boolean resetPassword(University university) throws PtplatformException {
        try {
            University universityMessage = universityDao.findById(university.getId()).orElse(null);
            assert universityMessage != null;
            universityMessage.setPassword(university.getPassword());
            universityDao.saveAndFlush(universityMessage);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

}
